package com.sakura.mall.member.member.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.member.member.domain.entity.MemberEntity;
import com.sakura.mall.member.member.domain.request.AddMemberRequest;
import com.sakura.mall.member.member.domain.request.UpdateMemberRequest;
import com.sakura.mall.member.member.domain.request.MemberPageRequest;
import com.sakura.mall.member.member.domain.vo.MemberVo;
import com.sakura.mall.member.member.mapper.MemberMapper;
import com.sakura.mall.member.member.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class MemberServiceImpl extends ServiceImpl<MemberMapper, MemberEntity> implements MemberService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddMemberRequest request) {
        // todo 其他校验逻辑
        MemberEntity member = request.convert(MemberEntity.class);
        this.save(member);
    }

    @Override
    public Page<MemberVo> queryPage(MemberPageRequest request) {
        IPage<MemberEntity> qp = request.toPage();
        LambdaQueryWrapper<MemberEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<MemberVo> ret = qp.convert(entity -> entity.convert(MemberVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateMemberRequest request) {
        MemberEntity entity = request.convert(MemberEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

